package com.tzraeq.quercus.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.tzraeq.quercus.StringServletInputStreamWrapper;

public class PreURIDecodeHttpServletRequestWrapper extends HttpServletRequestWrapper {

	public PreURIDecodeHttpServletRequestWrapper(HttpServletRequest request) {
		super(request);
	}
	
	public ServletInputStream getInputStream() throws IOException{
		InputStream is = getRequest().getInputStream();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buf = new byte[4096];
		int len = 0;
		while(-1!=(len=is.read(buf))){
			is.read(buf);
			baos.write(buf,0,len);
		}
		String temp = URLDecoder.decode(new String(baos.toByteArray()),"UTF-8");
		return new StringServletInputStreamWrapper(temp);
	}
	
}
